package device

import (
	"context"
	"fpga-vboard/pins"

	"github.com/wailsapp/wails/v2/pkg/runtime"
)

const (
	Switchs_Pin   = "switch_output"
	Switch_Number = 5
)

func init() {
	config := &SwitchsConfig{
		DeviceInfo: DeviceInfo{
			Id:        Switchs_DEFAULT,
			Type:      Switchs,
			InputPins: nil,
			Name:      "Switch Buttons",
			OutputPins: []*pins.PinDef{
				{
					Name:      Switchs_Pin,
					BitSize:   Switch_Number,
					Direction: pins.PinDirectionOutput,
				},
			},
			IsOptional: true,
			Enable:     false,
		},
		SwitchNumbers: Switch_Number,
	}
	RegisterDev(config)
}

type SwitchButtons struct {
	*DeviceBase
}

func NewSwitchButtons(config *SwitchsConfig) *SwitchButtons {
	return &SwitchButtons{
		DeviceBase: &DeviceBase{
			Config: config,
		},
	}
}

func (sw *SwitchButtons) SetValue(index uint, value bool) {
	// fmt.Printf("switchs: %d, %v\n", index, value)
	sw.SetPinBoolByIndex(Switchs_Pin, index, value)
}

func (sw *SwitchButtons) Start(ctx context.Context) error {
	runtime.EventsOn(ctx, "switchs", func(optionalData ...interface{}) {
		sw.SetValue(uint(optionalData[0].(float64)), optionalData[1].(bool))
	})
	return nil
}

func (sw *SwitchButtons) Stop(ctx context.Context) {
	runtime.EventsOff(ctx, "switchs")
}
